package other;


import org.junit.Test;

import java.util.Arrays;

/**
 * @description: 正常情况下对象比较只能==和!=，但开发场景中还是用用到>或<
 * 所有提供了Comparable和Comparator
 * @author: apple
 * @date:  2021/12/11 9:42 下午
 * @version 1.0
 **/
public class ComparableTest {

    @Test
    public void test1() {
        // 重新了Comparable接口，实现了CompareTo方法
        String[] names = {"c","d","e","a"};
        Arrays.sort(names);
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i]);
        }
    }

    @Test
    public void test2() {
        Goods[] goods = new Goods[4];
        goods[0] = new Goods(1, "联想笔记本",5800);
        goods[1] = new Goods(2, "苹果笔记本",18000);
        goods[2] = new Goods(3, "戴尔外星人",27000);
        goods[3] = new Goods(4, "ThinkPad-P15",7899);

        Arrays.sort(goods);
        for (int i = 0; i < goods.length; i++) {
            System.out.println(goods[i].getName() + "," + goods[i].getPrice());
        }

    }

}
